//
//  GYNicknameViewController.m
//  research
//
//  Created by 章鱼 on 17/3/29.
//  Copyright © 2017年 simon. All rights reserved.
//

#import "GYNicknameViewController.h"

@interface GYNicknameViewController ()

@property (nonatomic, strong) UITextField *nameTextfiel;
@property (nonatomic, strong) UIView *lineView;

@end

@implementation GYNicknameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self basicSetup];
    [self initNickNameView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self addBackButtonItemWithTitle:@"返回" backImg:@"navigation-bar_back_normal@2x"];
    [self addRightButtonItemWithTitle:@"完成" backImg:nil];

}

- (void)rightButtonItemClick {
    [super rightButtonItemClick];
    [self nickrightButtonClicked];
}


- (void)basicSetup {
    [self initNavBarWithTitle:_childViewTitle tintColor:[UIColor orz_mainColor]];
    [self.navBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                         [UIColor whiteColor],NSForegroundColorAttributeName,
                                         [UIFont boldSystemFontOfSize:20],
                                         NSFontAttributeName,
                                         nil]];
    [self.view setBackgroundColor:[UIColor orz_globalBackground]];
}


- (void)initNickNameView {
    _nameTextfiel = [[UITextField alloc] init];
    [self.view addSubview:_nameTextfiel];
    _nameTextfiel.font = [UIFont systemFontOfSize:13];
    _nameTextfiel.textAlignment = NSTextAlignmentLeft;
    _nameTextfiel.textColor = [UIColor colorWithHex:@"323232" alpha:1];
    [_nameTextfiel becomeFirstResponder];
    
    _lineView = [[UIView alloc] init];
    [self.view addSubview:_lineView];
    _lineView.backgroundColor = [UIColor colorWithHex:@"fed05b" alpha:1];
    
    _infoLable = [[UILabel alloc] init];
    [self.view addSubview:_infoLable];
    _infoLable.textColor = [UIColor colorWithHex:@"b5b5b5" alpha:1];
    _infoLable.font = [UIFont systemFontOfSize:13];
    
    if ([_childViewTitle isEqualToString:@"修改昵称"]) {
        _nameTextfiel.text = [[GYConfigDBManager defaultManager] userInformation].nickname;
        _infoLable.text = @"请您输入新的昵称";

    }
    if ([_childViewTitle isEqualToString:@"修改地址"]) {
        _nameTextfiel.text = [[GYConfigDBManager defaultManager] userInformation].address;
        _infoLable.text = @"请您输入新的地址";

    }
    if ([_childViewTitle isEqualToString:@"修改电话"]) {
        _nameTextfiel.text = [[GYConfigDBManager defaultManager] userInformation].phone;
        _infoLable.text = @"请您输入新的电话";

    }
    if ([_childViewTitle isEqualToString:@"修改邮箱"]) {
        _nameTextfiel.text = [[GYConfigDBManager defaultManager] userInformation].email;
        _infoLable.text = @"请您输入新的邮箱";

    }

    [self initNickNameViewUI];
}

- (void)initNickNameViewUI {
    
    [_nameTextfiel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(23.f);
        make.right.mas_equalTo(0.f);
        make.height.mas_equalTo(45.f);
        make.top.mas_equalTo(64.f);
    }];
    
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(12.f);
        make.right.mas_equalTo(-12.f);
        make.top.mas_equalTo(_nameTextfiel.mas_bottom);
        make.height.mas_equalTo(1.f);
    }];
    
    [_infoLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(23.f);
        make.top.mas_equalTo(_lineView.mas_bottom).offset(10.f);
    }];
    
}

#pragma mark - action
-(void)nickrightButtonClicked {

    if ([_childViewTitle isEqualToString:@"修改昵称"]) {
        [[YGHttpManager defaultManager] fetchGetUserSettingWithtoken:[[GYConfigDBManager defaultManager] gyToken] userId:[NSString stringWithFormat:@"%ld",(long)[[GYConfigDBManager defaultManager] userInformation].id] nickname:_nameTextfiel.text phone:[[GYConfigDBManager defaultManager] userInformation].phone email:[[GYConfigDBManager defaultManager] userInformation].email address:[[GYConfigDBManager defaultManager] userInformation].address gender:[[GYConfigDBManager defaultManager] userInformation].gender success:^(id responseObj) {
            GYCommonModel *commonmodel = [GYCommonModel mj_objectWithKeyValues:responseObj];
            if (commonmodel.status == 1) {
                
                [VBAlertView showTextHUD:@"修改成功"];
                GYUserInfoModel * model = [GYUserInfoModel mj_objectWithKeyValues:commonmodel.data];
                [[GYConfigDBManager defaultManager] setUserInformation:model];
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                [VBAlertView showTextHUD:commonmodel.message];
            }
        } failure:^(NSError *error) {
            
        }];

    }
    if ([_childViewTitle isEqualToString:@"修改地址"]) {
        [[YGHttpManager defaultManager] fetchGetUserSettingWithtoken:[[GYConfigDBManager defaultManager] gyToken] userId:[NSString stringWithFormat:@"%ld",(long)[[GYConfigDBManager defaultManager] userInformation].id] nickname:[[GYConfigDBManager defaultManager] userInformation].nickname phone:[[GYConfigDBManager defaultManager] userInformation].phone email:[[GYConfigDBManager defaultManager] userInformation].email address:_nameTextfiel.text gender:[[GYConfigDBManager defaultManager] userInformation].gender success:^(id responseObj) {
            GYCommonModel *commonmodel = [GYCommonModel mj_objectWithKeyValues:responseObj];
            if (commonmodel.status == 1) {
                
                [VBAlertView showTextHUD:@"修改成功"];
                GYUserInfoModel * model = [GYUserInfoModel mj_objectWithKeyValues:commonmodel.data];
                [[GYConfigDBManager defaultManager] setUserInformation:model];
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                [VBAlertView showTextHUD:commonmodel.message];

            }
        } failure:^(NSError *error) {
            
        }];

    }
    
    if ([_childViewTitle isEqualToString:@"修改电话"]) {
        [[YGHttpManager defaultManager] fetchGetUserSettingWithtoken:[[GYConfigDBManager defaultManager] gyToken] userId:[NSString stringWithFormat:@"%ld",(long)[[GYConfigDBManager defaultManager] userInformation].id] nickname:[[GYConfigDBManager defaultManager] userInformation].nickname phone:_nameTextfiel.text email:[[GYConfigDBManager defaultManager] userInformation].email address:[[GYConfigDBManager defaultManager] userInformation].address gender:[[GYConfigDBManager defaultManager] userInformation].gender success:^(id responseObj) {
            GYCommonModel *commonmodel = [GYCommonModel mj_objectWithKeyValues:responseObj];
            if (commonmodel.status == 1) {
                
                [VBAlertView showTextHUD:@"修改成功"];
                GYUserInfoModel * model = [GYUserInfoModel mj_objectWithKeyValues:commonmodel.data];
                [[GYConfigDBManager defaultManager] setUserInformation:model];
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                [VBAlertView showTextHUD:commonmodel.message];

            }
        } failure:^(NSError *error) {
            
        }];
        
    }
    
    if ([_childViewTitle isEqualToString:@"修改邮箱"]) {
        [[YGHttpManager defaultManager] fetchGetUserSettingWithtoken:[[GYConfigDBManager defaultManager] gyToken] userId:[NSString stringWithFormat:@"%ld",(long)[[GYConfigDBManager defaultManager] userInformation].id] nickname:[[GYConfigDBManager defaultManager] userInformation].nickname phone:[[GYConfigDBManager defaultManager] userInformation].phone email:_nameTextfiel.text address:[[GYConfigDBManager defaultManager] userInformation].address gender:[[GYConfigDBManager defaultManager] userInformation].gender success:^(id responseObj) {
            GYCommonModel *commonmodel = [GYCommonModel mj_objectWithKeyValues:responseObj];
            if (commonmodel.status == 1) {
                
                [VBAlertView showTextHUD:@"修改成功"];
                GYUserInfoModel * model = [GYUserInfoModel mj_objectWithKeyValues:commonmodel.data];
                [[GYConfigDBManager defaultManager] setUserInformation:model];
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                [VBAlertView showTextHUD:commonmodel.message];
                
            }
        } failure:^(NSError *error) {
            
        }];
    }
}

@end
